//
//  TapMarkerPopView.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2017/3/8.
//  Copyright © 2017年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "TapMarkerPopView.h"
#import "Public.h"
#import "UIView+AeroExtension.h"

@interface TapMarkerPopView()

@property(nonatomic,copy)void(^tapConfirmAction)(NSString *lon,NSString *lat);
@property(nonatomic,copy)void(^deleteAction)(void);
@end
@implementation TapMarkerPopView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        
        self.backgroundColor = [UIColor clearColor];
//        self.alpha = 0.3;
        [self createUI];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self hidden];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat space = 5;
    
    self.bgView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    self.title.frame = CGRectMake(0, 5, self.bgView.frame.size.width, 30);
    
    self.lon.frame = CGRectMake(0, CGRectGetMaxY(self.title.frame) + space, 50, 45);
    
    self.lonFiled.frame = CGRectMake(50, self.lon.frame.origin.y, self.bgView.frame.size.width-self.lon.frame.size.width-10, self.lon.frame.size.height);
    
    self.lat.frame = CGRectMake(0, CGRectGetMaxY(self.lon.frame) + space, CGRectGetWidth(self.lon.frame), CGRectGetHeight(self.lon.frame));
    
    self.latFiled.frame = CGRectMake(CGRectGetMaxX(self.lat.frame), self.lat.frame.origin.y, self.lonFiled.frame.size.width, self.lonFiled.frame.size.height);
    
    self.line.frame = CGRectMake(0, CGRectGetMaxY(self.lat.frame) + space, self.bgView.frame.size.width, 0.5);
    
    self.cancle.frame = CGRectMake(0, CGRectGetMaxY(self.line.frame), self.bgView.frame.size.width/2-0.5, 45);
    
    self.line1.frame = CGRectMake(CGRectGetMaxX(self.cancle.frame), self.cancle.xmg_y, 0.5, self.cancle.xmg_height);
    
    self.confirm.frame = CGRectMake(self.line1.xmg_right, self.cancle.frame.origin.y, self.cancle.frame.size.width, self.cancle.frame.size.height);
}

- (void)createUI{

    //背景
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 8;
    [self addSubview:self.bgView];
    
    //标题
    self.title = [[UILabel alloc] init];
    self.title.font = [UIFont systemFontOfSize:15];
    self.title.textColor = AEROMAP_RBG(3, 169, 244, 1.0);
    self.title.textAlignment = NSTextAlignmentCenter;
    self.title.text = @"标题1";
    [self.bgView addSubview:self.title];
    
    
    //经度
    self.lon = [[UILabel alloc] init];
    self.lon.font = [UIFont systemFontOfSize:15];
    self.lon.textColor = [UIColor lightGrayColor];
    self.lon.textAlignment = NSTextAlignmentCenter;
    self.lon.text = @"经度";
    [self.bgView addSubview:self.lon];
    
    //纬度
    self.lat = [[UILabel alloc] init];
    self.lat.font = [UIFont systemFontOfSize:15];
    self.lat.textColor = [UIColor lightGrayColor];
    self.lat.textAlignment = NSTextAlignmentCenter;
    self.lat.text = @"纬度";
    [self.bgView addSubview:self.lat];
    
    //经度输入框
    self.lonFiled = [[UITextField alloc] init];
    self.lonFiled.textColor = [UIColor lightGrayColor];
    self.lonFiled.borderStyle = UITextBorderStyleRoundedRect;
    self.lonFiled.textAlignment = NSTextAlignmentLeft;
    self.lonFiled.font = [UIFont systemFontOfSize:15];
    [self.bgView addSubview:self.lonFiled];
    
    //纬度输入框
    self.latFiled = [[UITextField alloc] init];
    self.latFiled.textColor = [UIColor lightGrayColor];
    self.latFiled.borderStyle = UITextBorderStyleRoundedRect;
    self.latFiled.textAlignment = NSTextAlignmentLeft;
    self.latFiled.font = [UIFont systemFontOfSize:15];
    [self.bgView addSubview:self.latFiled];
    
    //分割线
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:self.line];
    
    //取消按钮
    self.cancle = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancle setTitle:@"删除" forState:UIControlStateNormal];
    [self.cancle setTitleColor:AEROMAP_RBG(3, 169, 244, 1.0) forState:UIControlStateNormal];
    self.cancle.backgroundColor = [UIColor clearColor];
    [self.cancle addTarget:self action:@selector(cancaleAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.cancle];
    
    //分割线
    self.line1 = [[UIView alloc] init];
    self.line1.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:self.line1];
    
    //确定按钮
    self.confirm = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirm setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirm setTitleColor:AEROMAP_RBG(3, 169, 244, 1.0) forState:UIControlStateNormal];
    self.confirm.backgroundColor = [UIColor clearColor];
    [self.confirm addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.confirm];
    
    
//    self.title.backgroundColor = [UIColor orangeColor];
//    self.lon.backgroundColor = [UIColor orangeColor];
//    self.lonFiled.backgroundColor = [UIColor orangeColor];
//    self.lat.backgroundColor = [UIColor orangeColor];
//    self.latFiled.backgroundColor = [UIColor orangeColor];
//    self.line.backgroundColor = [UIColor orangeColor];
//    self.cancle.backgroundColor = [UIColor orangeColor];
//    self.confirm.backgroundColor = [UIColor orangeColor];
}

- (void)confirm:(void (^)(NSString *, NSString *))confirmAction{

    _tapConfirmAction = [confirmAction copy];
}

- (void)deletePoint:(void(^)(void))deleteAction{
    
    _deleteAction = [deleteAction copy];
}


- (void)cancaleAction{

    [self hidden];
    
    _deleteAction();
}

- (void)confirmAction{
    
    [self hidden];
    
    _tapConfirmAction(self.lonFiled.text,self.latFiled.text);
}

- (void)hidden{
    
    [self endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.origin.x, [UIScreen mainScreen].bounds.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
